package n13.first;

import java.util.*;

public class F01 {
	private static Map<Person, Set<Person>>map;
	static {
		map=new HashMap<Person, Set<Person>>();
	}
	public static void showContact(Person who) {
		System.out.println(who);
		Set<Person> set = map.get(who);
		if(set!=null) {
			set.forEach(p->System.out.println("\t"+p));
		}
	}
	public static void showContact() {
		map.keySet().forEach(p->showContact(p));
	}
	public static int addContact(Person who, Set<Person> contacts) {
		Set<Person> set = map.computeIfAbsent(who, p->new HashSet<Person>());
		int renshu = set.size(); //原来的人数
		set.addAll(contacts);
		return set.size()-renshu; //成功添加联系人的个数
	}
	public static int addContact(Person who, Person contact) {
		Set<Person> set = map.computeIfAbsent(who, p->new HashSet<Person>());
		return set.add(contact)?1:0;
//		Set<Person> set = map.get(who);
//		if(set==null) {
//			set = new HashSet<Person>();
//			map.put(who, set);
//		}
//		boolean success = set.add(contact);
//		return success?1:0;
	}
	public static void main(String[] args) {
		
		Person p1 = new Person("张三","13333333333");
		addContact(p1, new Person("李四","14444444444"));
		int count = addContact(p1, new Person("李四","14444444444"));
		System.out.println(count);
		addContact(p1, new Person("王五","15555555555"));
		showContact(p1);
		p1 = new Person("yangliu","16666666666");
		addContact(p1, new Person("zhaoqi","17777777777"));
		System.out.println("-------------------------");
		showContact();
		System.out.println("-------------------------");
		addContact(p1, map.get(new Person("张三","13333333333")));
		showContact();
	}

}
